package com.imti.mall.order.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.imti.mall.model.order.pojo.OmsOrder;
import com.imti.mall.model.order.query.OmsOrderDelivery;
import com.imti.mall.model.order.query.OmsOrderQuery;
import com.imti.mall.model.order.vo.OmsOrderAndItem;
import com.imti.mall.model.order.vo.OmsOrderMoneyInfo;
import com.imti.mall.model.order.vo.OmsOrderReceiverInfo;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 订单自定义查询Mapper
 */

@Mapper
@Transactional
public interface OrderMapper extends BaseMapper<OmsOrder> {

    /**
     * 条件查询订单
     */
    List<OmsOrder> getList(@Param("queryParam") OmsOrderQuery omsOrderQuery);

    /**
     * 根据订单id删除订单
     * @param id
     * @return
     */
   @Update("UPDATE `oms_order` SET delete_status=1 WHERE id=#{id}")
   int deletebyId(Long id);

    /**
     * 根据订单id查订单以及订单商品
     * @param omsOrderQuery
     * @return
     */
    List<OmsOrderAndItem> getOrderById(@Param("queryParam") OmsOrderQuery omsOrderQuery);

    /**
     * 根据订单id对订单进行备注
     * @param omsOrder
     * @return
     */
    @Update("UPDATE `oms_order` SET note=#{note} WHERE id=#{id}")
    int noteById(OmsOrder omsOrder);


    /**
     * 根据订单id关闭订单
     * @param omsOrder
     * @return
     */
   @Update("UPDATE `oms_order` SET status=5 WHERE id=#{id}")
   int closeOrderById(OmsOrder omsOrder);

    /**
     * 发货，插入快递公司以及单号,修改订单状态
     * @param omsOrderDelivery
     * @return
     */
   @Update("UPDATE `oms_order` SET delivery_company=#{deliveryCompany},delivery_sn=#{deliverySn},STATUS=2 WHERE id=#{orderId}")
   int updateOrderForDelivery(OmsOrderDelivery omsOrderDelivery);

    /**
     * 修改收货信息
     * @param omsOrderReceiverInfo
     * @return
     */
     @Update({ "<script>"+
           "UPDATE `oms_order` set" +
           " receiver_name=#{receiverName}, "+
           "<if test =\"receiverPhone!=null and receiverPhone!=''\"> "+
           " receiver_phone=#{receiverPhone}, "+
           "</if> "+
           "<if test =\"receiverPostCode!=null and receiverPostCode!=''\"> "+
           " receiver_post_code=#{receiverPostCode}, "+
           "</if> "+
           "<if test =\"receiverProvince!=null and receiverProvince!=''\"> "+
           " receiver_province=#{receiverProvince}, "+
           "</if> "+
           "<if test =\"receiverRegion!=null and receiverRegion!=''\"> "+
           " receiver_region=#{receiverRegion}, "+
           "</if> "+
           "<if test =\"receiverCity!=null and receiverCity!=''\"> "+
           " receiver_city=#{receiverCity}, "+
           "</if> "+
           "<if test =\"receiverDetailAddress!=null and receiverDetailAddress!=''\"> "+
           " receiver_detail_address=#{receiverDetailAddress} "+
           "</if> "+
           " WHERE " +
           " status=1" +
           " and id=#{orderId}" +
           "</script>"})
    int updateReceiverInfo(OmsOrderReceiverInfo omsOrderReceiverInfo);

    /**
     * 修改订单金额
     * @param omsOrderMoneyInfo
     * @return
     */
     int updateOrderMoney(OmsOrderMoneyInfo omsOrderMoneyInfo);



   /**
     * 修改支付状态
     * @param orderSn 订单编号
     * @param payType 支付方式
     * @return
     */
    @Update("update oms_order set pay_type=#{payType},payment_time=now(),status=1 where order_sn=#{orderSn}")
    int updateOrderPay(@Param("orderSn") String orderSn,@Param("payType") Integer payType);


    int insertAll(OmsOrder omsOrder);


}
